package com.zhongketech.sunflower;

import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.database.AppInfo;
import com.zhongketech.sunflower.filter.AppManager;
import com.zhongketech.sunflower.util.Constants;
import com.zhongketech.sunflower.util.Utilities;
import com.zhongketech.sunflower.view.StartAppAdater;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;



public class StartAppActivity extends BaseActivity {

	private final static String TAG = "StartAppActivity";
	private static Context context = null;


	private MyHandler myHandler = null;

	/**
	 * View init
	 */
	private ListView listView = null;



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.startapp);
		context = this;
		//custom
		myHandler = new MyHandler();
		initView();
	}





	private void initView()
	{

		listView = (ListView) findViewById(R.id.applistview);
		listView.setOnItemClickListener(onItemClickListener);
		new GetAppAsyncTask().execute((Void)null);
		if(!thread.isAlive())
		{
			thread.start();	
		}

	}

	class GetAppAsyncTask extends AsyncTask<Void, Void, String> {  

		@Override 
		protected String doInBackground(Void... params) {  
			AppManager.getInstance(context);
			Utilities.logCat(TAG, "doInBackground o", Constants.LOG_ONLYSHELL);
			return null;
		}  

		@Override 
		protected void onPostExecute(String result) {  
			super.onPostExecute(result);   
			Utilities.logCat(TAG, "onPostExecute start", Constants.LOG_ONLYSHELL);
			listView.setAdapter(new StartAppAdater(context));
		}  

	} 


	Thread thread = new Thread(new Runnable()  
	{  
		@Override  
		public void run()  
		{   

			while(true)
			{
				// TODO Auto-generated method stub  
				try {
					Thread.sleep(Constants.THREAD_INTERVAL);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				final String packages = getRunPackage();
				if(null != packages)
				{
					//Utilities.logCat(TAG, "run package: "+packages, Constants.LOG_ONLYSHELL);
					AppInfo appInfo = AppManager.getInstance(context).getAppInfo(packages);
					//Utilities.logCat(TAG, "appInfo: "+appInfo, Constants.LOG_ONLYSHELL);
					if(null != appInfo && appInfo.isStop)
					{
						Message message = new Message();  
						message.what = Constants.UPDATE_STOPAPP;  
						message.obj = appInfo.appName;
						myHandler.sendMessage(message);  
						Utilities.killProcess(packages, context);

					}
				}
			}



		}  
	});  

	private String getRunPackage()
	{
		final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);  
		final ComponentName cn = am.getRunningTasks(1).get(0).topActivity;  
		return  cn.getPackageName(); 
	}

	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
		}

	};

	/**
	 * 数据 更新操作
	 */
	static class MyHandler extends Handler
	{

		@Override
		public void handleMessage(Message msg) 
		{

			//Utilities.logCat(TAG, "msg.what "+msg.what+" msg.arg1: "+msg.arg1+" msg.arg2: "+msg.arg2);
			switch (msg.what) {
			case Constants.UPDATE_STOPAPP:
				Intent intent = new Intent();  
				intent.putExtra("packageName", (String)msg.obj);
				intent.setClass(MyApplication.getAppContext(), StopAppActivity.class);
				//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
				//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
				MyApplication.getAppContext().startActivity(intent);

				//Toast.makeText(MyApplication.getAppContext(), "aaa被禁止启动",Toast.LENGTH_SHORT).show();
			default:
				break;
			}
		}

	}
}
